//
//  MainTabBarController.m
//  梦香菜谱
//
//  Created by imac on 15/9/28.
//  Copyright (c) 2015年 imac. All rights reserved.
//

#import "MainTabBarController.h"

// 包含的控制器
#import "HomeVC.h"
#import "NoticeBoardVC.h"
#import "PersonalVC.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏系统的TabBar
    self.tabBar.hidden = YES;
    //创建定自定义的TabBar
    [self _createTabBar];
    
    [self _createViewControllers];
}

//创建自定义的TabBar
- (void)_createTabBar {
    
    //未选中状态图片
    NSArray *imageNames = @[
                            @"首页",
                            @"公告栏",
                            @"我的",
                            ];
    //选中状态图片
    NSArray *selectedImageNames = @[
                                    @"首页_选择",
                                    @"公告栏_选择",
                                    @"我的_选择",
                                    ];
    //标签栏标题
    NSArray *titles = @[
                        @"首页",
                        @"公告栏",
                        @"我的",
                        ];
    
    //创建整个TabBar
    _tabBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-49, SCREEN_WIDTH, 49)];
    self.tabBarView.backgroundColor = [UIColor whiteColor];
    _tabBarView.userInteractionEnabled = YES;
    _tabBarView.contentMode = UIViewContentModeScaleToFill;
    // 添加分隔线
    UIView *separateLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    separateLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [_tabBarView addSubview:separateLine];
    [self.view addSubview:_tabBarView];
    
    //创建tabBar按钮
    CGFloat buttonWidth = SCREEN_WIDTH / 3;
    for (int i = 0; i < 3; i++) {
        //未选中图片
        UIImage *buttonImage = [UIImage imageNamed:imageNames[i]];
        //选中图片
        UIImage *selectedImage = [UIImage imageNamed:selectedImageNames[i]];
        TabBarButton *button = [[TabBarButton alloc] initWithFrame:CGRectMake(i * buttonWidth, 0, buttonWidth, 49) Image:buttonImage SelectedImage:selectedImage Title:titles[i]];
        button.tag = i;
        if (i == 0) {
            //刚开始设置第一个按钮为选中按钮
            button.isSelectedButton = YES;
            self.selectedButton = button;
        }
        [button addTarget:self action:@selector(_buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBarView addSubview:button];
    }
}

//tabBar按钮点击事件
- (void)_buttonAction:(TabBarButton *)button {
    //切换视图控制器
    self.selectedIndex = button.tag;
    //上一次选中的按钮状态改为no
    self.selectedButton.isSelectedButton = NO;
    //当前被点击的按钮状态改为yes
    button.isSelectedButton = YES;
    //记录当前选中按钮
    self.selectedButton = button;    
}

#pragma mark -- 创建控制器 --
- (void)_createViewControllers {
    
    HomeVC *homeVC = [[HomeVC alloc] init];
    BaseNavVC *navi1 = [[BaseNavVC alloc] initWithRootViewController:homeVC];
    NoticeBoardVC *noticeBoardVC = [[NoticeBoardVC alloc] init];
    BaseNavVC *navi2 = [[BaseNavVC alloc] initWithRootViewController:noticeBoardVC];
    PersonalVC *personalVC = [[PersonalVC alloc] init];
    BaseNavVC *navi3 = [[BaseNavVC alloc] initWithRootViewController:personalVC];
    
    self.viewControllers = @[navi1,navi2,navi3];
}

#pragma mark - 标签栏隐藏和显示
// 隐藏标签栏
- (void)hideTabBar
{
    [UIView animateWithDuration:0.3 animations:^{
        _tabBarView.top = SCREEN_HEIGHT;
    }];
}


// 标签栏出现
- (void)showTabBar
{
    [UIView animateWithDuration:0.3 animations:^{
        _tabBarView.bottom = SCREEN_HEIGHT;
    }];
}
@end
